<template>
  <div class="login">
    <el-form
      class="form"
      ref="form"
      :model="form"
      :rules="rules"
      @keyup.enter.native="submit"
    >
      <h2
        class="text-center"
        style="margin-bottom: 20px;"
      >登录</h2>
      <el-form-item prop="account">
        <el-input
          size="default"
          v-model="form.username"
          prefix-icon="el-icon-user"
        />
      </el-form-item>
      <el-form-item prop="pwd">
        <el-input
          size="default"
          v-model="form.password"
          prefix-icon="el-icon-lock"
          show-password
        />
      </el-form-item>
      <el-form-item>
        <el-button
          size="default"
          type="primary"
          style="width: 100%;"
          :loading="loading"
          @click="submit"
        >登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { login } from "@/api";

export default {
  data() {
    return {
      loading: false,
      form: {
        username: "",
        password: ""
      },
      rules: {
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" }
        ],
        password: [{ required: true, message: "密码不能为空", trigger: "blur" }]
      }
    };
  },
  methods: {
    submit() {
      this.$submit(this, "form", () => {
        this.loading = true;
        login(
          this.form,
          res => {
            if (res.code * 1 === -1) {
              this.loading = false;
              this.form.password=""
            } else {
              this.$router.replace({ name: "main" });
            }
          },
          () => {
            alert(1)
            this.loading = false;
          }
        );
      });
    }
  }
};
</script>
<style lang="scss">
.login {
  padding-top: 150px;
  height: 100%;
  background-image: url("./login.jpg");
  background-repeat: no-repeat;
  background-size: cover;
  min-height: 570px;

  .form {
    margin: 0 auto;
    width: 400px;
    background-color: #ffffff;
    padding: 20px 40px;
    border-radius: 4px;
    box-shadow: 0 0 30px #ddd;
  }
}
</style>
